<template>
  <page-header-wrapper :title="false">
    <a-card title="评估师管理">
      <template slot="extra">
        <a-button icon="plus" size="small" type="primary" @click="onAdd">添加评估师</a-button>
      </template>
      <div class="table-operator">
        <s-table
          bordered
          ref="table"
          size="middle"
          :row-key="(record) => record.id"
          :columns="appraiserColumns"
          :data="appraiserData"
        >
          <template slot="action" slot-scope="text,record">
            <a-button size="small" icon="edit" type="danger" ghost @click="onEdit(record.id)">编辑</a-button>
            <a-button size="small" icon="delete" type="danger" @click="delAppraiser(record.id)">删除</a-button>
          </template>
        </s-table>
      </div>
    </a-card>
    <a-modal
      :confirm-loading="loading"
      title="评估师信息"
      v-model="visible"
      width="400px"
      @ok="onSubmit"
    >
      <a-form-model
        ref="form"
        :model="appraiserForm"
      >
        <a-form-model-item label="评估师编号">
          <a-input v-model="appraiserForm.num" type="text" />
        </a-form-model-item>
        <a-form-model-item label="评估师姓名">
          <a-input v-model="appraiserForm.username" type="text" />
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
import { STable } from '@/components'
import taskColumns from '@/columns/pg.process'
import { delAppraiser, getAddAppraiser, getAppraiser, postAddAppraiser } from '@/api/pg.process'

export default {
  components: {
    STable
  },
  name: 'Appraiser',
  data () {
    return {
      loading: false,
      visible: false,
      appraiserForm: {},
      appraiserColumns: taskColumns.appraiser,
      appraiserData: parameters => {
        return getAppraiser(Object.assign(parameters, this.queryParam)).then(res => {
          return res.result
        })
      },
      labelCol: { span: 6 },
      wrapperCol: { span: 18 }
    }
  },
  methods: {
    onAdd () {
      this.appraiserForm = {}
      this.visible = true
    },
    onSubmit () {
      const app = this
      const data = {}
      app.loading = true
      for (const k in app.appraiserForm) {
        data['appraiser[' + k + ']'] = app.appraiserForm[k]
      }
      if (app.appraiserForm.id) {
        data['id'] = app.appraiserForm.id
        postAddAppraiser(data).then(res => {
          app.$refs.table.refresh(true)
          app.loading = false
          app.visible = false
        })
      } else {
        data['id'] = ''
        postAddAppraiser(data).then(res => {
          app.$refs.table.refresh(true)
          app.loading = false
          app.visible = false
        })
      }
    },
    onEdit (id) {
      this.appraiserForm = {}
      getAddAppraiser(id).then(res => {
        this.appraiserForm = res.result.appData
        this.visible = true
      })
    },
    delAppraiser (id) {
      this.$confirm({
        title: '是否确认删除',
        onOk: () => {
          delAppraiser(id).then(res => {
            this.$refs.table.refresh(true)
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
